﻿using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Modules.Couple.Interfaces;
using StockholmsStad.Windows.UI.Modules.Couple.Models;
using System.Windows.Controls;

namespace StockholmsStad.Windows.UI.Modules.Couple.Views
{
    /// <summary>
    /// Interaction logic for TabItemExtras.xaml
    /// </summary>
    public partial class TabItemExtrasView : IExtrasView
    {
        private int loadedTicketId;
        private readonly IExtrasService extrasService;

        public TabItemExtrasView(IExtrasService service)
        {
            InitializeComponent();
            extrasService = service;
            Loaded += TabItemExtrasView_Loaded;
            extrastTextBox.KeyUp += extrastTextBox_KeyUp;

            // We need to initialize in order to set the tab name properly
            Model = service.GetExtras(loadedTicketId);

            PreviewLostKeyboardFocus += TabItemExtrasView_PreviewLostKeyboardFocus;
        }

        private void TabItemExtrasView_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            var newfocus = e.NewFocus;
            if (newfocus.GetType() == typeof(TabItem))
            {
                var tabItem = (TabItem)newfocus;
                if (!tabItem.Header.ToString().Equals(HeaderNames.Extras))
                {
                    //Byte till annan tab
                    e.Handled = HandleChange();
                }
            }
            if (newfocus.GetType() == typeof(ListViewItem)) 
            {
                var listViewItem = (ListViewItem)newfocus;
                if (listViewItem.Content != null &&
                    listViewItem.Content is CouplesSummaryItem)
                {
                    //Assume it's the couples list view 
                    e.Handled = HandleChange();
                }
            }
        }

        private void TabItemExtrasView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (loadedTicketId != SelectedTicketId)
            {
                Model = extrasService.GetExtras(selectedTicketId);
                loadedTicketId = selectedTicketId;
            }
        }

        #region IExtrasView Members

        public ExtrasPresentationModel Model
        {
            get { return DataContext as ExtrasPresentationModel; }
            set { DataContext = value; }
        }

        private int selectedTicketId;
        public int SelectedTicketId
        {
            get { return selectedTicketId; }
            set
            {
                selectedTicketId = value;
                if (IsLoaded && loadedTicketId != SelectedTicketId)
                {
                    Model = extrasService.GetExtras(selectedTicketId);
                    loadedTicketId = selectedTicketId;
                }
            }
        }

        #endregion

        private void ToggleButtons()
        {
            var toggle = !extrastTextBox.Text.Equals(Model.ExtrasInfo);
            saveButton.IsEnabled = cancelButton.IsEnabled = Model.IsInChangeMode = toggle;
        }

        private void extrastTextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            ToggleButtons();
        }
        private bool HandleChange()
        {
            if (Model.IsInChangeMode)
            {
                var result = MessageBox.Show(Properties.Resources.UnsavedChanges, "Information", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                if (result == MessageBoxResult.Yes)
                {
                    Model.IsInChangeMode =false;
                }
                else
                {
                    return true;
                }
            }
            return false;
        }
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if(!Model.TicketId.HasValue)
            {
                MessageBox.Show("Du måste skapa ett ärende först. ", "Ärende saknas", MessageBoxButton.OK,
                                MessageBoxImage.Information);
                return;
            }

            BindingExpression be = extrastTextBox.GetBindingExpression(TextBox.TextProperty);
            if(be != null) be.UpdateSource();
            ToggleButtons();
        }

        private void cancelButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            extrastTextBox.Text = Model.ExtrasInfo;
            ToggleButtons();
        }
    }
}
